//
//  ChattingViewController.m
//  BaChat
//
//  Created by 刘洋 on 16/6/16.
//  Copyright © 2016年 刘洋. All rights reserved.
//

#import "ChattingViewController.h"
#import "BottomControlView.h"
#import "MyTableViewCell.h"
#import "OppositeTableViewCell.h"
#import "EMCDDeviceManager.h"
#import "PlusMoreView.h"
#import "FaceView.h"

static NSString *myCell = @"myCell";
static NSString *oppositeCell = @"oppositeCell";

@interface ChattingViewController ()<UITableViewDataSource,UITableViewDelegate,EMChatManagerDelegate,UIScrollViewDelegate,UITextViewDelegate>

@property(nonatomic,retain)BaseTableView *chattingTableView;

@property(nonatomic,retain)BaseView *bottomView;

//会话类
@property(nonatomic,retain)EMConversation *conversation;

@property(nonatomic,retain)UITextField *chattingTextField;

@property(nonatomic,retain)BottomControlView *myBottomControlView;

@property(nonatomic,retain)PlusMoreView *morePlusView;

@property(nonatomic,retain)FaceView *myFaceView;

@property(nonatomic,retain)NSMutableArray *messageArray;

@property(nonatomic,assign)CGFloat textViewHeight;
@property(nonatomic,assign)CGFloat keyBoardHeight;

@end

@implementation ChattingViewController

-(void)dealloc{
    [_myFaceView release];
    [_messageArray release];
    [_myBottomControlView release];
    [_oppositeName release];
    [_chattingTableView release];
    [_bottomView release];
    [_chattingTextField release];
    [super dealloc];
}

#pragma mark -- 页面加载之前,将tableView至低(显示最新聊天记录)
-(void)viewWillAppear:(BOOL)animated{
  
    //调用聊天记录方法
  [self showChatLog];
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = self.oppositeName;
    
    self.navigationController.navigationBar.translucent = NO;
    

    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[[UIImage imageNamed:@"navigationbar_back_light"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(leftAction)];
    
    [[EaseMob sharedInstance].chatManager addDelegate:self delegateQueue:dispatch_get_main_queue()];


    [self addNotificationCenterForKeyboard];
    

    self.chattingTableView = [[BaseTableView alloc]initWithFrame:CGRectMake(0, 0, ROOTWIDTH, ROOTHEIGHT ) style:UITableViewStylePlain];


  //  self.chattingTableView.backgroundColor = RGBAColor(239, 239, 244);
    self.chattingTableView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.chattingTableView];
    [_chattingTableView release];
    
    [self.chattingTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.mas_equalTo(self.view);
        make.bottom.mas_equalTo(self.view).offset(-50);
        
    }];
    [self.chattingTableView registerClass:[MyTableViewCell class] forCellReuseIdentifier:myCell];
    [self.chattingTableView registerClass:[OppositeTableViewCell class] forCellReuseIdentifier:oppositeCell];
    
    self.chattingTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.chattingTableView.delegate = self;
    self.chattingTableView.dataSource = self;
    
    
    //拖动时隐藏键盘
    self.chattingTableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
    
    [self.chattingTableView setAllowsSelection:NO];
    
    self.textViewHeight = 40;
    
    //添加输入框控制器
    self.myBottomControlView = [[BottomControlView alloc]init];
    self.myBottomControlView.backgroundColor = RGBAColor(239, 239, 249);
    [self.view addSubview:self.myBottomControlView];
    
    //设置输入框的约束
    [self.myBottomControlView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        NSLog(@"初始约束");
        make.left.right.mas_equalTo(self.view);
        make.bottom.mas_equalTo(self.view);
        make.height.mas_equalTo(self.textViewHeight + 10);
        
        
    }];


    
    self.myBottomControlView.messageTextView.delegate = self;
    
    

    
    [self morePlusButtonBlock];
    
    [self voiceButtonBlock];

    [self touchVoiceStartAndFinishBlock];
    
    [self faceViewBlock];
    

    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(sendAddress:) name:@"sendAddress" object:nil];

    
    // Do any additional setup after loading the view.
}


-(void)leftAction{
    [self.conversation markAllMessagesAsRead:YES];
    
    [self.navigationController popViewControllerAnimated:YES];
    
}




#pragma mark -- 接受信息的方法
-(void)didReceiveMessage:(EMMessage *)message{
   
    
    [self showChatLog];
    
    
}



#pragma mark -- 聊天记录
-(void)showChatLog{
   //创建一个单人会话对象
    self.conversation = [[EaseMob sharedInstance].chatManager conversationForChatter:self.oppositeName conversationType:eConversationTypeChat]; //单人会话
    
    [[EaseMob sharedInstance].chatManager insertConversationToDB:self.conversation append2Chat:YES];

    
    self.conversation.enableUnreadMessagesCountEvent = YES;
    

   
    
//[self.conversation loadAllMessages]  会话的所有内容 是数组
    
    //加载当前用户的所有聊天记录
    self.messageArray = [NSMutableArray arrayWithArray:[self.conversation loadAllMessages]];

    
    [self.chattingTableView reloadData];

    [self LYTableViewScrollToBottom];
    
}




-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.messageArray.count;
    
}



-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    EMMessage *message = self.messageArray[indexPath.row];

//如果发送对象是当前的聊天对象
    if ([message.to isEqualToString:self.oppositeName]) {
        MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:myCell];
        
        cell.myMessage = message;
        
        return cell;
        
    }else{
        
//如果不是,就是对方发送来的消息
        OppositeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:oppositeCell];
        cell.myMessage = message;
        return cell;
    }
    
    

    
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    EMMessage *message = [self.messageArray objectAtIndex:indexPath.row];
    
    id bodyClass = [message.messageBodies lastObject];
    
    if ([bodyClass isKindOfClass:[EMVoiceMessageBody class]]) {
        
         return 100;
        
    }else if ([bodyClass isKindOfClass:[EMImageMessageBody class]]){
        
        return 200;
    }else if ([bodyClass isKindOfClass:[EMVideoMessageBody class]]){
        
        return 200;
    }else if ([bodyClass isKindOfClass:[EMLocationMessageBody class]]){
        
        return 130;
    }
    
    
    
    EMTextMessageBody *textBody = bodyClass;
    
    if ([message.to isEqualToString:self.oppositeName]) {
        
       return [MyTableViewCell heightOfCellWithtextMessge:textBody];
    
    }else{

//    CGFloat height = [MyTableViewCell heightForCellWithSizeWidth:200 withString:textBody.text withLabelFont:17];
//    
//    return height + 20;

    return [OppositeTableViewCell heightOfCellWithtextMessge:textBody];
    
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"22222");
    
}






- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark -- 发送消息的方法(参数是textField的text)
-(void)sendMessageFromBottomTextFiled:(NSString *)text{
    //要发送的消息
    EMChatText *chatText = [[EMChatText alloc]initWithText:text];
    EMTextMessageBody *body = [[EMTextMessageBody alloc]initWithChatObject:chatText];
 
    [chatText release];
    
    EMMessage *message = [[EMMessage alloc]initWithReceiver:self.oppositeName bodies:@[body]];
    message.messageType = eMessageTypeChat; //设置为单聊消息
    
    //发送消息
 [[EaseMob sharedInstance].chatManager asyncSendMessage:message progress:nil prepare:^(EMMessage *message, EMError *error) {
     
     if (!error) {
         NSLog(@"准备发送");
     }
     
     
 } onQueue:dispatch_get_main_queue() completion:^(EMMessage *message, EMError *error) {
     
     if (!error) {
         NSLog(@"发送完成");
         //刷新聊天记录
         [self showChatLog];
     }
     
     
 } onQueue:dispatch_get_main_queue()];

    
    
    
    
}



#pragma mark --创建通知中心
-(void)addNotificationCenterForKeyboard{
    
//键盘将要弹起
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didKeyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    
//键盘将要隐藏
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didkeyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    
}


#pragma mark -- 键盘将要出现时
-(void)didKeyboardWillShow:(NSNotification *)noti{
    
    NSLog(@"键盘弹起");
    
    NSDictionary *info = [noti userInfo];
    
    CGSize keyboardSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
    
    self.keyBoardHeight = keyboardSize.height;
    
    //调用键盘弹起视图上升方法
    [self bottomViewShowAnimationWithKeyboardOffset:keyboardSize.height withTextViewHeight:self.textViewHeight ];
    
    
    
    
    
    
}


#pragma mark -- 键盘将要隐藏时
-(void)didkeyboardWillHide:(NSNotification *)noti{
    NSLog(@"键盘隐藏");
    [self bottomViewShowAnimationWithKeyboardOffset:0 withTextViewHeight:self.textViewHeight];
    
    
}

#pragma mark --输入框控制器的弹起动画和收回动画,以及tableView的弹起收回
-(void)bottomViewShowAnimationWithKeyboardOffset:(CGFloat)setHeight withTextViewHeight:(CGFloat)textViewHeight{

//更新textView的高度
    [self.myBottomControlView.messageTextView mas_updateConstraints:^(MASConstraintMaker *make) {
      
        make.height.mas_equalTo(self.textViewHeight);

 
    }];
    
    
    
    
//键盘的弹出,和收起时,更新输入框的高度
    [self.myBottomControlView mas_updateConstraints:^(MASConstraintMaker *make) {
        
        make.bottom.mas_equalTo(- setHeight);
        make.height.mas_equalTo(self.textViewHeight + 10);
        
        
    }];


    
    
//更新tableView的约束
    [self.chattingTableView mas_updateConstraints:^(MASConstraintMaker *make) {
        
        if (setHeight == 0) {
            
            NSLog(@"%.2f",textViewHeight);
            make.bottom.mas_equalTo(self.view).offset(- textViewHeight - 10);
            
        }else{
            NSLog(@"%.2f",textViewHeight);
            make.bottom.mas_equalTo(self.view).offset(- textViewHeight - 10 - setHeight);
        }
        
        
        
    }];

    

    
    
    
    
//实现动画效果
    [UIView animateWithDuration:0.25 animations:^{
        
        [self.view layoutIfNeeded];
        [self.chattingTableView layoutIfNeeded];
        
        
    }];
    
    
    [self LYTableViewScrollToBottom];
    
    
    
    if (setHeight != 0) {
      
        
        return;
    }
    
    
//    [self.morePlusView mas_makeConstraints:^(MASConstraintMaker *make) {
//        
//        make.bottom.mas_equalTo(self.view.mas_bottom).offset(- ROOTHEIGHT / 3);
//        make.left.right.mas_equalTo(self.view);
//        
//        
//    }];
    
    self.morePlusView.frame = CGRectMake(0, ROOTHEIGHT, ROOTWIDTH, ROOTHEIGHT / 3);

    self.myFaceView.frame = CGRectMake(0, ROOTHEIGHT, ROOTWIDTH, ROOTHEIGHT / 3);
    
}



#pragma mark -- UITextView的协议方法  根据输入内容适应高度
-(void)textViewDidChange:(UITextView *)textView{
    


    CGFloat minHeight = 40;  //40 为最小宽度
    CGFloat maxHeight = 90;  //80 为最小宽度
    
    
    //获取contentSize的高度, 就能知道输入的内容的高度
    CGFloat contentHeight = textView.contentSize.height;
    if (contentHeight < minHeight) {
        
        self.textViewHeight = minHeight;
        
        
    }else if (contentHeight > maxHeight){

        self.textViewHeight = maxHeight;
        
    }else{

        self.textViewHeight = contentHeight;

    }


    
//监听send事件 发送键相当于空格
    if ([textView.text hasSuffix:@"\n"]) {
        [self sendMessageFromBottomTextFiled:textView.text];
        
        textView.text = nil;
        
        self.textViewHeight = minHeight;
        
        
        
    }
    
    
    //让光标回到原位
    [textView setContentOffset:CGPointZero animated:YES];
    //光标回到偏移量为0的地方
    [textView scrollRangeToVisible:textView.selectedRange];
    
    
    
//更新textView的约束
    [self.myBottomControlView.messageTextView mas_updateConstraints:^(MASConstraintMaker *make) {
        

        make.height.mas_equalTo(self.textViewHeight);
          NSLog(@"22~~~~~~111111");
        NSLog(@"%.2f",self.textViewHeight);
    }];
    
//刷新输入框的约束
    [self.myBottomControlView mas_updateConstraints:^(MASConstraintMaker *make) {
       
        make.height.mas_equalTo(self.textViewHeight + 10);
    

    }];
 
//刷新tableView的约束
    [self.chattingTableView mas_updateConstraints:^(MASConstraintMaker *make) {
        
        make.bottom.mas_equalTo(self.view).offset(- self.textViewHeight - self.keyBoardHeight);
        
    }];
    
   
   
    
    
    [self LYTableViewScrollToBottom];
    
}


-(void)LYTableViewScrollToBottom{
   
    
    if (self.messageArray.count > 0) {
        
        [self.chattingTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:self.messageArray.count - 1 inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:YES];
    }
    
 
}

#pragma mark -- 语音按钮的block回调
-(void)voiceButtonBlock{
    
    __block ChattingViewController *chatting = self;
    self.myBottomControlView.voiceBlock = ^(BaseButton *voiceButton){
    
            if (voiceButton.selected == NO) {
                
                [voiceButton setBackgroundImage:[UIImage imageNamed:@"message_keyboard_background"] forState:UIControlStateNormal];
        
                [chatting.myBottomControlView.messageTextView setContentOffset:CGPointZero animated:YES];
                

                //键盘回收
                [chatting.view endEditing:YES];
              
//更新约束
            [chatting.myBottomControlView.messageTextView mas_updateConstraints:^(MASConstraintMaker *make) {
                
                make.height.mas_equalTo(40);
                
                NSLog(@"11~~~~~~111111");
                
               }];

//更新textView的约束
             [chatting.myBottomControlView mas_updateConstraints:^(MASConstraintMaker *make) {
                 
                 make.height.mas_equalTo(50);
                 
                 NSLog(@"$$$$$4");
             }];
                

                
//更新TableView的约束
            [chatting.chattingTableView mas_updateConstraints:^(MASConstraintMaker *make) {
                    make.bottom.mas_equalTo(-50);
                    
                }];
                
               
                [UIView animateWithDuration:0.25 animations:^{
                    
                [chatting.view layoutIfNeeded];
                 
                //更换成按住说话按钮
                [chatting.myBottomControlView createTouchVoiceButton];
                    
                }];
                
                NSLog(@"33333333333");
                
        
            }else{
                
                [voiceButton setBackgroundImage:[UIImage imageNamed:@"message_privatechat_voice_background_highlighted"] forState:UIControlStateNormal];
        
                [chatting.myBottomControlView.messageTextView becomeFirstResponder];
                
                [chatting.myBottomControlView.touchVoiceButton removeFromSuperview];
                
            }
            voiceButton.selected = !voiceButton.selected;
    
    };
}

#pragma mark --录制语音的block回调
-(void)touchVoiceStartAndFinishBlock{
    NSLog(@"开始就走了");
    
    __block ChattingViewController *chatting = self;
//开始录音
    self.myBottomControlView.touchVoiceStartButtonBlock = ^(BaseButton *touchStartButton){
    
        touchStartButton.backgroundColor = [UIColor lightGrayColor];
        [touchStartButton setTitle:@"松开 结束" forState:UIControlStateNormal];
        NSLog(@"开始录音按钮");

    //文件名以时间为名
        int x = arc4random() % 100000;
        NSTimeInterval time = [[NSDate date] timeIntervalSince1970];
        NSString *fileVoiceName = [NSString stringWithFormat:@"%d%d",(int)time,x];
        NSLog(@"%@",fileVoiceName);
    
     [[EMCDDeviceManager sharedInstance] asyncStartRecordingWithFileName:fileVoiceName completion:^(NSError *error) {
         
         if (!error) {
             NSLog(@"开始录音成功");
             
             BOOL isRecord = [[EMCDDeviceManager sharedInstance]isRecording];
             NSLog(@"%d",isRecord);
             
    NSInteger volume =[[EMCDDeviceManager sharedInstance] emPeekRecorderVoiceMeter];
             NSLog(@"-------@%ld",volume);
             
             //启动MB动画
             [LYSupportTools LYMBHUDAnimationForRecordingAddToView:self.view];
             
         }
         
         
     }];


    };
    
//完成录音
    self.myBottomControlView.touchVoiceFinishButtonBlock = ^(BaseButton *touchFinishButton){
        
        touchFinishButton.backgroundColor = [UIColor whiteColor];
         [touchFinishButton setTitle:@"按住 说话" forState:UIControlStateNormal];
        
        NSLog(@"完成录音按钮");
   
        //停止所有动画
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
        
        [[EMCDDeviceManager sharedInstance] asyncStopRecordingWithCompletion:^(NSString *recordPath, NSInteger aDuration, NSError *error) {
            
            if (!error) {
                NSLog(@"录音完成");
                
                NSLog(@"%@ %ld",recordPath,aDuration);
                
                //发送语音给服务器
                [self sendVoiceMessage:recordPath withDuration:aDuration];
                
                
                
            }else{
            
                //小于一秒时无法录音,提示感叹号
                
                [[EMCDDeviceManager sharedInstance] cancelCurrentRecording];
                
               //调用LYSupportTools的加号方法
                [LYSupportTools LYMBHUDCustomViewForExclamationPointAddToView:self.view];
                
                
            }
  
        }];
  
  
    
        
    };

       
}



#pragma mark -- 封装一个,往环信发送语音消息的方法
-(void)sendVoiceMessage:(NSString *)recordPath withDuration:(NSInteger)duration{
    
    //创建一个语音对象
//    displayName 是显示在列表中告诉用户点钱最近的消息是语音类型
    EMChatVoice *chatVoice = [[EMChatVoice alloc]initWithFile:recordPath displayName:@"[语音]"];
    
    //创建语音消息体
    EMVoiceMessageBody *voiceBody = [[EMVoiceMessageBody alloc]initWithChatObject:chatVoice];
    [chatVoice release];
    voiceBody.duration = duration;
   
    //调用发送任意消息的方法
    [self sendAllClassMessage:voiceBody];
    
}

#pragma mark -- 可以发送所有类型消息
-(void)sendAllClassMessage:(id<IEMMessageBody>)body{
    //创建消息对象(消息体)
    EMMessage *messageClass = [[EMMessage alloc]initWithReceiver:self.oppositeName bodies:@[body]];
    messageClass.messageType = eMessageTypeChat;
    
    //发送消息
    [[EaseMob sharedInstance].chatManager asyncSendMessage:messageClass progress:nil

prepare:^(EMMessage *message, EMError *error) {
    NSLog(@"准备发送");
    
} onQueue:nil completion:^(EMMessage *message, EMError *error) {
    
    NSLog(@"发送成功");
} onQueue:nil];
    
    
    
 //将消息显示在tableView上
    [self.messageArray addObject:messageClass];
 //刷新tableView
    [self.chattingTableView reloadData];
    
 //消息置低
    [self LYTableViewScrollToBottom];
    
    [messageClass release];
    
    
}

#pragma mark -- 添加点击更多弹出的collectionView
-(void)morePlusCollectionView{
    
    self.morePlusView = [[PlusMoreView alloc]init];
    [self.view addSubview:self.morePlusView ];
    self.morePlusView .backgroundColor = [UIColor whiteColor];

    
    __block ChattingViewController *chattingVC = self;
    //选取图片或者是拍照的block
    self.morePlusView.selectedImageBlock = ^(UIImage *selectedImage){
        
        NSLog(@"%@",selectedImage);
        
        [chattingVC sendImage:selectedImage];
        
    };

    //选取视频的block
    self.morePlusView.localPathBlock = ^(NSString *localPath){
        
        
        [chattingVC sendVideo:localPath];
        
        
    };
    
    //语音转文字的block
    self.morePlusView.voiceToTextViewBlock = ^(NSString *textString){
        
        self.myBottomControlView.messageTextView.text = textString;
        
    };
  
   
    
    
    [_morePlusView release];

    self.morePlusView.frame = CGRectMake(0, self.view.frame.size.height, ROOTWIDTH, ROOTHEIGHT / 3);
    
   
    
}

#pragma mark -- 更多按钮的回调
-(void)morePlusButtonBlock{
    
    //调用添加点击更多弹出的collectionView的方法
 //   [self morePlusCollectionView];
    

    self.myBottomControlView.morePlusButtonBlock = ^(BaseButton *moreButton){
        

         if (moreButton.selected == NO) {
             
             NSLog(@"更多出现");
              [self.view endEditing:YES];
             
        
             
             //调用添加点击更多弹出的collectionView的方法
             [self morePlusCollectionView];
             
             [self.myBottomControlView mas_updateConstraints:^(MASConstraintMaker *make) {
                 make.bottom.mas_equalTo(self.view.mas_bottom).offset(-ROOTHEIGHT / 3);
                
                 
             }];
             
             [self.chattingTableView mas_updateConstraints:^(MASConstraintMaker *make) {
                 
                 make.bottom.mas_equalTo(self.view.mas_bottom).offset(-ROOTHEIGHT / 3 - self.textViewHeight - 10);
             }];
             
            
             
             [UIView animateWithDuration:0.4 animations:^{
                 
            //     [self.view layoutIfNeeded];
                 [self.myBottomControlView layoutIfNeeded];
                 [self.chattingTableView layoutIfNeeded];
                 
                 self.morePlusView.frame = CGRectMake(0, self.view.frame.size.height - ROOTHEIGHT / 3, ROOTWIDTH, ROOTHEIGHT / 3);
        
             }];
             
              [self LYTableViewScrollToBottom];
             
         }else{
             
             NSLog(@"更多消失");
             
        
             
             [self.myBottomControlView.messageTextView becomeFirstResponder];
             
             [self.morePlusView mas_updateConstraints:^(MASConstraintMaker *make) {
                 
                 make.top.mas_equalTo(self.view.mas_bottom).offset(0);
        
                 
             }];
             
         }
        
         moreButton.selected = !moreButton.selected;
         
 
        
    };
    
    
}

#pragma mark -- 添加表情view
-(void)createFaceView{
    
    self.myFaceView = [[FaceView alloc]initWithFrame:CGRectMake(0, self.view.frame.size.height, ROOTWIDTH, ROOTHEIGHT / 3)];
    self.myFaceView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.myFaceView];
   
    __block ChattingViewController *chatting = self;
    self.myFaceView.faceStringBlock = ^(NSString *faceStr){
        
        
        NSString *textStr = chatting.myBottomControlView.messageTextView.text;
        
        chatting.myBottomControlView.messageTextView.text = [textStr stringByAppendingString:faceStr];
        ;
        
        
        
    };
    
     [_myFaceView release];
    
}


-(void)faceViewBlock{
    
    self.myBottomControlView.faceButtonBlock = ^(BaseButton *faceButton){

        
            if (faceButton.selected == NO) {
                
                NSLog(@"更多出现");
                [self.view endEditing:YES];
                
            
                //调用添加点击更多弹出的collectionView的方法
                [self createFaceView];
                
                [self.myBottomControlView mas_updateConstraints:^(MASConstraintMaker *make) {
                    make.bottom.mas_equalTo(self.view.mas_bottom).offset(-ROOTHEIGHT / 3);
                    
                    
                }];
                
                [self.chattingTableView mas_updateConstraints:^(MASConstraintMaker *make) {
                    
                    make.bottom.mas_equalTo(self.view.mas_bottom).offset(-ROOTHEIGHT / 3 - self.textViewHeight - 10);
                }];
                
                
                
                [UIView animateWithDuration:0.4 animations:^{
                    
                    //     [self.view layoutIfNeeded];
                    [self.myBottomControlView layoutIfNeeded];
                    [self.chattingTableView layoutIfNeeded];
                    
                    self.myFaceView.frame = CGRectMake(0, self.view.frame.size.height - ROOTHEIGHT / 3, ROOTWIDTH, ROOTHEIGHT / 3);
                    
                }];
                
                [self LYTableViewScrollToBottom];
                
            }else{
                
                NSLog(@"更多消失");
                
                
                
                [self.myBottomControlView.messageTextView becomeFirstResponder];
                
                [self.myFaceView mas_updateConstraints:^(MASConstraintMaker *make) {
                    
                    make.top.mas_equalTo(self.view.mas_bottom).offset(0);
                    
                    
                }];
                
            }
            
            faceButton.selected = !faceButton.selected;
            
            
            
        };
        

    
}




#pragma mark -- 发送图片消息
-(void)sendImage:(UIImage *)selectedImage{
    
    
    //1.构造图片消息体
    /*
     * 第一个参数：原始大小的图片对象 1000 * 1000
     * 第二个参数: 缩略图的图片对象  120 * 120
     */
    
    EMChatImage *orginalImage = [[EMChatImage alloc]initWithUIImage:selectedImage displayName:@"[图片]"];


    
    EMImageMessageBody *imageBody = [[EMImageMessageBody alloc]initWithImage:orginalImage thumbnailImage: orginalImage];
    
    [self sendAllClassMessage:imageBody];
    
    
}

#pragma mark -- 发送视频消息
-(void)sendVideo:(NSString *)localPath{
    
    
    EMChatVideo *video = [[EMChatVideo alloc]initWithFile:localPath displayName:@"[视频]"];
    video.shouldGenerateThumbnail = YES;
    
    EMVideoMessageBody *videoBody = [[EMVideoMessageBody alloc]initWithChatObject:video];
    
    [self sendAllClassMessage:videoBody];
    
    
}


#pragma mark -- 发送位置通知的实现
-(void)sendAddress:(NSNotification *)noti{
    
    NSDictionary *addressDic = [noti object];
    
    NSLog(@"++++%@",addressDic);
    
    //latitude纬度 longitude
    
    double latitude = [[addressDic objectForKey:@"latitude"] doubleValue];
    
    double longitude = [[addressDic objectForKey:@"longitude"] doubleValue];
    
    
    EMChatLocation *loaction = [[EMChatLocation alloc]initWithLatitude:latitude  longitude:longitude address:@"[地址]"];
    
    EMLocationMessageBody *body = [[EMLocationMessageBody alloc]initWithChatObject:loaction];
    
    [self sendAllClassMessage:body];
    

}




@end
